package com.szyjjk.service;

import com.szyjjk.entity.*;
import com.szyjjk.mapper.*;
import com.szyjjk.util.DateUtil;
import com.szyjjk.util.HttpTool;
import com.szyjjk.util.UUIDUtils;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: szyjjk
 * @description: "硬件接口表"数据服务 加一波数据入库
 * @author: yangyue
 * @create: 2020-05-19 15:02
 */
@Service
@Transactional(readOnly = true)
public class SysHearWordService {

	@Autowired
	private SysHardWareMapper sysHardWareMapper;

	@Autowired
	private SysHardWareFileMapper sysHardWareFileMapper;

	@Autowired
	private SysHardWareDataMapper sysHardWareDataMapper;

	@Autowired
	private SysHardWareSimulationDataMapper sysHardWareSimulationDataMapper;

	@Autowired
	private RunHisDataMapper runHisDataMapper;

	@Autowired
	private RunPropertyMapper runPropertyMapper;

	/**
	 * @return java.util.List<com.szyjjk.entity.SysHardWare>
	 * @Author yangyue
	 * @Description //TODO 查询全部硬件网站数据
	 * @Date 15:08 2020/5/19
	 * @Param []
	 **/
	public List<SysHardWare> findHareWareAll() {
		return sysHardWareMapper.selectByExample(null);
	}

	/**
	 * @return java.util.List<com.szyjjk.entity.SysHardWareFile>
	 * @Author yangyue
	 * @Description //TODO 根据名称和时间查询
	 * @Date 9:59 2020/5/20
	 * @Param [Name, UploadTime]
	 **/
	public List<SysHardWareFile> findFileHardByTimeName(String Name, Date UploadTime) {
		SysHardWareFileExample example = new SysHardWareFileExample();
		example.createCriteria().andFileNameEqualTo(Name).andFileUploadTimeEqualTo(UploadTime);
		return sysHardWareFileMapper.selectByExample(example);
	}

	/**
	 * @Author yangyue
	 * @Description //TODO 根据属性和时间查询
	 * @Date 11:34 2020/9/28
	 * @Param [pro, proTime]
	 * @return java.util.List<com.szyjjk.entity.SysHardWareData>
	 **/
	public List<SysHardWareData> finProByTimeName(String pro, Date proTime) {
		SysHardWareDataExample example = new SysHardWareDataExample();
		example.createCriteria().andDataNameEnEqualTo(pro).andDataTimeEqualTo(proTime);
		return sysHardWareDataMapper.selectByExample(example);
	}

	/**
	 * @return boolean
	 * @Author yangyue
	 * @Description //TODO 判断是否存在重复的数据
	 * @Date 15:08 2020/5/20
	 * @Param [fileName, date]
	 **/
	public boolean existsHardWareData(String fileName, Date date) {
		SysHardWareDataExample example = new SysHardWareDataExample();
		example.createCriteria().andDataFileNameEqualTo(fileName).andDataTimeEqualTo(date);
		return sysHardWareDataMapper.selectByExample(example).size() > 0;
	}

	/**
	 * @return java.util.List<com.szyjjk.entity.SysHardWareFile>
	 * @Author yangyue
	 * @Description //TODO 根据文件名查询文件，在排序
	 * @Date 14:22 2020/5/20
	 * @Param [Name]
	 **/
	public List<SysHardWareFile> findFileHardByName(String Name) {
		SysHardWareFileExample example = new SysHardWareFileExample();
		example.createCriteria().andFileNameEqualTo(Name);
		example.setOrderByClause("file_upload_time desc ");
		return sysHardWareFileMapper.selectByExample(example);
	}

	/**
	 * @return java.util.List<com.szyjjk.entity.SysHardWareData>
	 * @Author yangyue
	 * @Description //TODO 根据时间范围和属性中文名查询
	 * @Date 17:27 2020/5/20
	 * @Param [Name, beginTime, endTime]
	 **/
	public List<SysHardWareData> findSysHarwareDateByNameBeginTime(String Name, Date beginTime, Date endTime) {
		SysHardWareDataExample example = new SysHardWareDataExample();
		if (beginTime == null) {
			example.createCriteria().andDataNameChEqualTo(Name);
		} else {
			example.createCriteria().andDataNameChEqualTo(Name).andDataTimeBetween(beginTime, endTime);
		}
		example.setOrderByClause("data_time asc ");
		return sysHardWareDataMapper.selectByExample(example);
	}
	public List<SysHardWareData> findSysHarwareDateByNameBeginTime(String[] sitearr,String name, Date beginTime, Date endTime) {
		SysHardWareDataExample example = new SysHardWareDataExample();
		if (beginTime == null) {
			example.createCriteria().andDataNameChEqualTo(name);
		} else {
			example.createCriteria().andDataNameChEqualTo(name).andDataTimeBetween(beginTime, endTime);
		}
		example.setOrderByClause("data_time asc ");
		return sysHardWareDataMapper.selectbyremarknameandtime(sitearr,name,beginTime,endTime);
	}
	public List<SysHardWareSimulationData> findSysHarwaresimulationDateByNameBeginTime(String[] sitearr,String name, Date beginTime, Date endTime) {
		SysHardWareSimulationDataExample example = new SysHardWareSimulationDataExample();
		if (beginTime == null) {
			example.createCriteria().andDataNameChEqualTo(name);
		} else {
			example.createCriteria().andDataNameChEqualTo(name).andDataTimeBetween(beginTime, endTime);
		}
		example.setOrderByClause("data_time asc ");
		return sysHardWareSimulationDataMapper.selectbyremarknameandtime(sitearr,name,beginTime,endTime);
	}
	public List<SysHardWareData> findSysHarwareDateByBeginTime(Date beginTime, Date endTime) {
		SysHardWareDataExample example = new SysHardWareDataExample();
		if (beginTime != null) {
			example.createCriteria().andDataTimeBetween(beginTime, endTime);
		}
		example.setOrderByClause("data_time asc ");
		return sysHardWareDataMapper.selectByExample(example);
	}

	/**
	 * @return java.lang.Boolean
	 * @Author yangyue
	 * @Description //TODO 保存文件数据 分开传输
	 * @Date 14:52 2020/5/20
	 * @Param [timedata, dataValue, nameCh, nameEn, flieName]
	 **/
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public Boolean saveSysHardWareDate(Date timedata, String dataValue, String nameCh, String nameEn, String flieName) {
		SysHardWareData sysHardWareData = new SysHardWareData();
		sysHardWareData.setId(HttpTool.getRandomChar(32));
		sysHardWareData.setDataTime(timedata);
		sysHardWareData.setDataNameCh(nameCh);
		sysHardWareData.setDataNameEn(nameEn);
		sysHardWareData.setDataValue(dataValue);
		sysHardWareData.setDataFileName(flieName);
		try {
			// todo 可填加后续逻辑
			sysHardWareDataMapper.insertSelective(sysHardWareData);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	/**
	 * @return java.lang.Boolean
	 * @Author yangyue
	 * @Description //TODO 保存文件数据
	 * @Date 14:52 2020/5/20
	 * @Param [sysHardWareData]
	 **/
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public Boolean saveSysHardWareDate(SysHardWareData sysHardWareData) {
		try {
			// todo 可填加后续逻辑
			sysHardWareDataMapper.insertSelective(sysHardWareData);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public Boolean saveSysHardWareDate(List<SysHardWareData> list) {
		try {
			// todo 可填加后续逻辑
			sysHardWareDataMapper.insertSelectives(list);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	/**
	 * @return java.lang.Boolean
	 * @Author yangyue
	 * @Description //TODO 保存
	 * @Date 17:17 2020/5/21
	 * @Param [runHisData]
	 **/
	@Transactional(readOnly = false)
	public Boolean saveRunHisData(RunHisData runHisData) {
		try {
			// todo 可填加后续逻辑
			runHisDataMapper.insertSelective(runHisData);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	/**
	 * @return java.util.List<com.szyjjk.entity.RunHisData>
	 * @Author yangyue
	 * @Description //TODO 查询全部histData、
	 * @Date 16:38 2020/5/25
	 * @Param []
	 **/
	public List<RunHisData> findAllHisData() {
		return this.runHisDataMapper.selectByExample(null);
	}

	/**
	 * @Author yangyue
	 * @Description //TODO 查询一个
	 * @Date 11:53 2020/9/27
	 * @Param [id]
	 * @return com.szyjjk.entity.RunHisData
	 **/
	public SysHardWareData findById(String id) {
		return this.sysHardWareDataMapper.selectByPrimaryKey(id);
	}

	/**
	 * @return java.lang.Boolean
	 * @Author yangyue
	 * @Description //TODO 修改hisdata
	 * @Date 16:36 2020/5/25
	 * @Param [runHisData]
	 **/
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public Boolean editRunHisData(RunHisData runHisData) {
		try {
			// todo 可填加后续逻辑
			runHisDataMapper.updateByPrimaryKeySelective(runHisData);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public Boolean editSysHardWareData(SysHardWareData runHisData) {
		try {
			// todo 可填加后续逻辑
			sysHardWareDataMapper.updateByPrimaryKeySelective(runHisData);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	/**
	 * @return java.util.List<com.szyjjk.entity.RunHisData>
	 * @Author yangyue
	 * @Description //TODO 查询hisdata
	 * @Date 14:11 2020/5/22
	 * @Param [remark, teri, fileID, begigtime, endTime]
	 **/
	public List<RunHisData> findHisByQuery(String remark, String teri, String fileID, Date begigtime, Date endTime,
			String site) {

		RunHisDataExample example = new RunHisDataExample();
		if (begigtime == null) {
			example.createCriteria().andFileidEqualTo(fileID).andHisRemarkEqualTo(remark)
					.andHisTierEqualTo(Integer.parseInt(teri)).andHisSiteEqualTo(site);

		} else {
			example.createCriteria().andFileidEqualTo(fileID).andHisRemarkEqualTo(remark)
					.andHisTierEqualTo(Integer.parseInt(teri)).andHisSiteEqualTo(site)
					.andHisTimeBetween(begigtime, endTime);
		}
		example.setOrderByClause("his_time asc ");
		return this.runHisDataMapper.selectByExample(example);
	}

	/**
	 * @return java.lang.Boolean
	 * @Author yangyue
	 * @Description //TODO 保存文件
	 * @Date 14:20 2020/5/20
	 * @Param [sysHardWareFile]
	 **/
	@Transactional(readOnly = false, propagation = Propagation.SUPPORTS)
	public Boolean saveSysHardWareFile(SysHardWareFile sysHardWareFile) {
		try {
			// todo 可填加后续逻辑
			sysHardWareFileMapper.insertSelective(sysHardWareFile);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	/**
	 * @return java.util.List<com.szyjjk.entity.RunHisData>
	 * @Author yangyue
	 * @Description //TODO 查询更具fileID
	 * @Date 11:11 2020/5/26
	 * @Param [id]
	 **/
	public List<RunHisData> findHisDataByfileID(String id) {
		RunHisDataExample example = new RunHisDataExample();
		example.createCriteria().andFileidEqualTo(id);
		return this.runHisDataMapper.selectByExample(example);
	}

	/**
	 * @return java.util.List<com.szyjjk.entity.RunProperty>
	 * @Author yangyue
	 * @Description //TODO 查询全部属性
	 * @Date 10:29 2020/6/8
	 * @Param []
	 **/
	public List<RunProperty> findProAll() {
		return runPropertyMapper.selectByExample(null);
	}

//    public RunProperty findChName(String name){
//        RunPropertyExample example = new RunPropertyExample();
//        example.createCriteria().and(name);
//        return this.runHisDataMapper.selectByExample(example);
//    }

	// 解析数据
	/**
	 * 解析his数据
	 * @param path
	 * @param id
	 * @return
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public int redBinary(String path, String id,String date1) throws FileNotFoundException, IOException {

		List<RunProperty> runPropertiesList = findProAll();
		List<RunHisData> runHisDataLists = this.findHisDataByfileID(id);
		if (runHisDataLists.size() != 0) {
			// 执行解析
			return 0;
		}
		DecimalFormat df = new DecimalFormat("#########.#");
		DecimalFormat df1 = new DecimalFormat("#########.####");
		DataInputStream dis = null;

		// test2.his: true
		// test2.map: false
		boolean bFileType = true;
		dis = new DataInputStream(new FileInputStream(path));

		dis.skip(124);

		byte[] bParamNameTime = new byte[19];
		String sParamNameTime = "";
		dis.read(bParamNameTime);
		sParamNameTime = new String(bParamNameTime);
		Date beginTime = null;
		try {
			beginTime = HttpTool.BinarySimpDate.parse(sParamNameTime);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		System.out.println(beginTime);

		dis.skip(17);

		byte[] bVarBUf = new byte[4];
		dis.read(bVarBUf);
		int iParamSize = ByteBuffer.wrap(bVarBUf).order(ByteOrder.LITTLE_ENDIAN).getInt();
		System.out.println(iParamSize);

		dis.read(bVarBUf);
		int iLocationSize = ByteBuffer.wrap(bVarBUf).order(ByteOrder.LITTLE_ENDIAN).getInt();
		System.out.println(iLocationSize);

		String sName = "";
		byte[] bParamName = new byte[20];
		String[] sParamName = new String[iParamSize];
		for (int i = 0; i < iParamSize; i++) {
			dis.read(bParamName);
			sParamName[i] = new String(bParamName);
			sName += sParamName[i];
		}
		String[] proArrayList = sName.split("          ");
		System.out.println(sName);

		byte[] bLocationName = new byte[20];
		String[] sLocationName = new String[iLocationSize];
		sName = "";
		for (int i = 0; i < iLocationSize; i++) {
			if (bFileType) {
				dis.read(bVarBUf);
				// System.out.println(ByteBuffer.wrap(b1).order(ByteOrder.LITTLE_ENDIAN).getInt());

				dis.read(bLocationName);
				sLocationName[i] = new String(bLocationName);
				sName += sLocationName[i];
			} else {
				sLocationName[i] = "segment " + df.format(i + 1);
			}
		}

		String rsultSout = "";
		List<RunHisData> runHisDataList = new ArrayList<>();
		while (dis.available() > 0) {
			dis.read(bVarBUf);
			int secound = ByteBuffer.wrap(bVarBUf).order(ByteOrder.LITTLE_ENDIAN).getInt();
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(beginTime);
			calendar.add(Calendar.SECOND, secound);

			Calendar date = Calendar.getInstance();
			
			date.setTime(DateUtil.stringToDate2(date1));
			
			calendar.set(Calendar.YEAR, date.get(Calendar.YEAR));

			System.out.println(calendar.getTime());
			System.out.println("---------------");
			for (int i = 0; i < iLocationSize; i++) {
				rsultSout = sLocationName[i] + "\t";
				String site = sLocationName[i].substring(0, 3);
				String tier = sLocationName[i].substring(6, 7);
				for (int j = 0; j < iParamSize; j++) {
					dis.read(bVarBUf);
					String values = df1.format(ByteBuffer.wrap(bVarBUf).order(ByteOrder.LITTLE_ENDIAN).getFloat());
					RunHisData runHisData = new RunHisData();
					runHisData.setId(HttpTool.getRandomChar(32));
					runHisData.setHisSite(site);
					if (!tier.contains(" ") && tier.length() != 0) {
						runHisData.setHisTier(Integer.parseInt(tier));
					}
					runHisData.setHisPro(proArrayList[j]);
					runHisData.setHisValue(values);
					runHisData.setHisTime(calendar.getTime());
					runHisData.setFileid(id);
					for (RunProperty runProperty : runPropertiesList) {
						String runSiteRo = proArrayList[j].trim();
						if (runSiteRo.equals(runProperty.getProNameen())) {
							runHisData.setHisRemark(runProperty.getProNamech());
							runHisData.setHisUnit(runProperty.getProUnit());
							break;
						}
					}
					rsultSout += values + "\t";

					if ("SecchiDept".equals(proArrayList[j].trim())) {
						runHisData.setHisPro("Transparence");
						for (RunProperty runProperty : runPropertiesList) {
							if ("Transparence".equals(runProperty.getProNameen())) {
								runHisData.setHisRemark(runProperty.getProNamech());
								runHisData.setHisUnit(runProperty.getProUnit());
								saveRunHisData(runHisData);
								break;
							}
						}
						runHisDataList.add(runHisData);
					} else {
						if (runHisData.getHisRemark() != null) {
							//this.saveRunHisData(runHisData);
							if ("TOC".equals(runHisData.getHisPro().trim())) {
								runHisData.setId(HttpTool.getRandomChar(32));
								runHisData.setHisPro("BOD");
								String hisValue = runHisData.getHisValue();
								double parseDouble = Double.parseDouble(hisValue);
								String format = df1.format(parseDouble * 0.63);
								runHisData.setHisValue(format);
								for (RunProperty runProperty : runPropertiesList) {
									if ("BOD".equals(runProperty.getProNameen())) {
										runHisData.setHisRemark(runProperty.getProNamech());
										runHisData.setHisUnit(runProperty.getProUnit());
										saveRunHisData(runHisData);
										break;
									}
								}
							}else {
								 if("EnCoc".equals(runHisData.getHisPro().trim())
										 ||"EColi".equals(runHisData.getHisPro().trim())
										 ||"FColi".equals(proArrayList[j].trim())){
									 double parseDouble1 = Double.parseDouble(runHisData.getHisValue());
									 if(parseDouble1!=0) {
										 runHisData.setHisValue(df1.format(Math.log10(parseDouble1)));
									 }
								}
								 saveRunHisData(runHisData);
							}
							runHisDataList.add(runHisData);
						}
					}
				}

			}

		}
		dis.close();
		return runHisDataList.size();
	}

	@Transactional(readOnly = false)
	public Integer delById(String id) {
		RunHisDataExample example = new RunHisDataExample();
		example.createCriteria().andFileidEqualTo(id);
		try {
			return runHisDataMapper.deleteByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 *   同化模型
	 * 
	 * @param path
	 * @param id
	 * @return
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	@Transactional(readOnly = false, propagation = Propagation.NEVER)
	public void redBinaryByYear(String path, String id) throws FileNotFoundException, IOException {

		List<RunProperty> runPropertiesList = findProAll();

		DecimalFormat df = new DecimalFormat("#########.#");
		DecimalFormat df1 = new DecimalFormat("#########.####");
		DataInputStream dis = null;

		// test2.his: true
		// test2.map: false
		boolean bFileType = true;
		dis = new DataInputStream(new FileInputStream(path));

		dis.skip(124);

		byte[] bParamNameTime = new byte[19];
		String sParamNameTime = "";
		dis.read(bParamNameTime);
		sParamNameTime = new String(bParamNameTime);
		Date beginTime = null;
		try {
			beginTime = HttpTool.BinarySimpDate.parse(sParamNameTime);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		// System.out.println(beginTime);

		dis.skip(17);

		byte[] bVarBUf = new byte[4];
		dis.read(bVarBUf);
		int iParamSize = ByteBuffer.wrap(bVarBUf).order(ByteOrder.LITTLE_ENDIAN).getInt();
		// System.out.println(iParamSize);

		dis.read(bVarBUf);
		int iLocationSize = ByteBuffer.wrap(bVarBUf).order(ByteOrder.LITTLE_ENDIAN).getInt();
		// System.out.println(iLocationSize);

		String sName = "";
		byte[] bParamName = new byte[20];
		String[] sParamName = new String[iParamSize];
		for (int i = 0; i < iParamSize; i++) {
			dis.read(bParamName);
			sParamName[i] = new String(bParamName);
			sName += sParamName[i];
		}
		String[] proArrayList = sName.split("          ");
		// System.out.println(sName);

		byte[] bLocationName = new byte[20];
		String[] sLocationName = new String[iLocationSize];
		sName = "";
		for (int i = 0; i < iLocationSize; i++) {
			if (bFileType) {
				dis.read(bVarBUf);
				// System.out.println(ByteBuffer.wrap(b1).order(ByteOrder.LITTLE_ENDIAN).getInt());

				dis.read(bLocationName);
				sLocationName[i] = new String(bLocationName);
				sName += sLocationName[i];
			} else {
				sLocationName[i] = "segment " + df.format(i + 1);
			}
		}

		String rsultSout = "";
		List<RunHisData> efdc1 = new ArrayList<>();
		List<RunHisData> efdc2 = new ArrayList<>();
		List<RunHisData> efdc3 = new ArrayList<>();
		List<RunHisData> efdc4 = new ArrayList<>();
		List<RunHisData> efdc5 = new ArrayList<>();
		int num = 500;

		while (dis.available() > 0) {
			dis.read(bVarBUf);
			int secound = ByteBuffer.wrap(bVarBUf).order(ByteOrder.LITTLE_ENDIAN).getInt();
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(beginTime);
			calendar.add(Calendar.SECOND, secound);

			Calendar date = Calendar.getInstance();
			date.setTime(new Date());
			calendar.set(Calendar.YEAR, date.get(Calendar.YEAR));

			// System.out.println(calendar.getTime());
			// System.out.println("---------------");
			for (int i = 0; i < iLocationSize; i++) {
				rsultSout = sLocationName[i] + "\t";
				String site = sLocationName[i].substring(0, 3);
				String tier = sLocationName[i].substring(6, 7);
				for (int j = 0; j < iParamSize; j++) {
					dis.read(bVarBUf);
					String values = df1.format(ByteBuffer.wrap(bVarBUf).order(ByteOrder.LITTLE_ENDIAN).getFloat());
					RunHisData runHisData = new RunHisData();
					runHisData.setId(HttpTool.getRandomChar(32));
					runHisData.setHisSite(site);
					if (!tier.contains(" ") && tier.length() != 0) {
						runHisData.setHisTier(Integer.parseInt(tier));
					}
					runHisData.setHisPro(proArrayList[j]);
					runHisData.setHisValue(values);
					runHisData.setHisTime(calendar.getTime());
					// runHisData.setHisTime(beginTime);
					runHisData.setFileid(id);
					for (RunProperty runProperty : runPropertiesList) {
						String runSiteRo = proArrayList[j].trim();
						if (runSiteRo.equals(runProperty.getProRemark())) {
							runHisData.setHisRemark(runProperty.getProNamech());
							runHisData.setHisUnit(runProperty.getProUnit());
						}
					}
					rsultSout += values + "\t";
					
					if ("SecchiDept".equals(proArrayList[j].trim())) {
						runHisData.setHisPro("Transparence");
						for (RunProperty runProperty : runPropertiesList) {
							if ("Transparence".equals(runProperty.getProNameen())) {
								runHisData.setHisRemark(runProperty.getProNamech());
								runHisData.setHisUnit(runProperty.getProUnit());
								saveListEfdc(efdc1, efdc2, efdc3, efdc4, efdc5, num, runHisData);
								break;
							}
						}
					} else {
						if ("TOC".equals(runHisData.getHisPro().trim())) {
							runHisData.setId(HttpTool.getRandomChar(32));
							runHisData.setHisPro("BOD");
							String hisValue = runHisData.getHisValue();
							double parseDouble = Double.parseDouble(hisValue);
							String format = df1.format(parseDouble * 0.63);
							runHisData.setHisValue(format);
							
							for (RunProperty runProperty : runPropertiesList) {
								if ("BOD".equals(runProperty.getProNameen())) {
									runHisData.setHisRemark(runProperty.getProNamech());
									runHisData.setHisUnit(runProperty.getProUnit());
									if (StringUtils.isNotBlank(runHisData.getHisRemark())) {
										saveListEfdc(efdc1, efdc2, efdc3, efdc4, efdc5, num, runHisData);
									}
									break;
								}
							}
						} else {
							 if("EnCoc".equals(runHisData.getHisPro().trim())
									 ||"EColi".equals(runHisData.getHisPro().trim())
									 ||"FColi".equals(proArrayList[j].trim())){
								 double parseDouble = Double.parseDouble(runHisData.getHisValue());
								 if(parseDouble!=0) {
									 runHisData.setHisValue(df1.format(Math.log10(parseDouble)));
								 }
							}
							if (StringUtils.isNotBlank(runHisData.getHisRemark())) {
								saveListEfdc(efdc1, efdc2, efdc3, efdc4, efdc5, num, runHisData);
							}
						}
					}

				}

			}

		}
		insertRunHisDataEfdc("run_his_data_efdc1", efdc1);
		insertRunHisDataEfdc("run_his_data_efdc2", efdc2);
		insertRunHisDataEfdc("run_his_data_efdc3", efdc3);
		insertRunHisDataEfdc("run_his_data_efdc4", efdc4);
		insertRunHisDataEfdc("run_his_data_efdc5", efdc5);
		dis.close();
	}

	@Transactional(readOnly = false, propagation = Propagation.NEVER)
	private void saveListEfdc(List<RunHisData> efdc1, List<RunHisData> efdc2, List<RunHisData> efdc3,
			List<RunHisData> efdc4, List<RunHisData> efdc5, int num, RunHisData runHisData) {
		if (runHisData.getHisSite().equals("DM1")) {
			efdc1.add(runHisData);
		} else if (runHisData.getHisSite().equals("DM2")) {
			efdc2.add(runHisData);
		} else if (runHisData.getHisSite().equals("DM3")) {
			efdc3.add(runHisData);
		} else if (runHisData.getHisSite().equals("DM4")) {
			efdc4.add(runHisData);
		} else {
			// 5 和 wate
			efdc5.add(runHisData);
		}

		if (efdc1.size() == num) {
			String tableName = "run_his_data_efdc1";
			insertRunHisDataEfdc(tableName, efdc1);
			efdc1.clear();
		}
		if (efdc2.size() == num) {
			String tableName = "run_his_data_efdc2";
			insertRunHisDataEfdc(tableName, efdc2);
			efdc2.clear();
		}
		if (efdc3.size() == num) {
			String tableName = "run_his_data_efdc3";
			insertRunHisDataEfdc(tableName, efdc3);
			efdc3.clear();
		}
		if (efdc4.size() == num) {
			String tableName = "run_his_data_efdc4";
			insertRunHisDataEfdc(tableName, efdc4);
			efdc4.clear();
		}
		if (efdc5.size() == num) {
			String tableName = "run_his_data_efdc5";
			insertRunHisDataEfdc(tableName, efdc5);
			efdc5.clear();
		}
	}

	@Transactional(readOnly = false, propagation = Propagation.NEVER)
	private void insertRunHisDataEfdc(String tableName, List<RunHisData> list) {
		try {
			if(CollectionUtils.isNotEmpty(list)) {
				runHisDataMapper.insertRunHisDataEfdc(list, tableName);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void delEfdcById(String id, String tableNameTou) {

		RunHisDataExample example = new RunHisDataExample();
		example.createCriteria().andFileidEqualTo(id);

		Map<String, String> map = new HashMap<>();
		map.put("hisId", id);

		runHisDataMapper.deleteByExample(example);

		for (int i = 1; i < 6; i++) {
			map.put("tableName", tableNameTou + i);
			try {
				runHisDataMapper.delEfdcById(map);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}
	/**
	 *  读取目录下一年his 数据
	 * @param path
	 * @param id
	 * @throws IOException
	 */
	@Transactional(readOnly = false, propagation = Propagation.NEVER)
	public void redBinaryByOneYear(String path, String id) throws IOException {

		List<RunProperty> runPropertiesList = findProAll();

		DecimalFormat df = new DecimalFormat("#########.#");
		DecimalFormat df1 = new DecimalFormat("#########.####");
		DataInputStream dis = null;

		// test2.his: true
		// test2.map: false
		boolean bFileType = true;
		dis = new DataInputStream(new FileInputStream(path));

		dis.skip(124);

		byte[] bParamNameTime = new byte[19];
		String sParamNameTime = "";
		dis.read(bParamNameTime);
		sParamNameTime = new String(bParamNameTime);
		Date beginTime = null;
		try {
			beginTime = HttpTool.BinarySimpDate.parse(sParamNameTime);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		// System.out.println(beginTime);

		dis.skip(17);

		byte[] bVarBUf = new byte[4];
		dis.read(bVarBUf);
		int iParamSize = ByteBuffer.wrap(bVarBUf).order(ByteOrder.LITTLE_ENDIAN).getInt();
		// System.out.println(iParamSize);

		dis.read(bVarBUf);
		int iLocationSize = ByteBuffer.wrap(bVarBUf).order(ByteOrder.LITTLE_ENDIAN).getInt();
		// System.out.println(iLocationSize);

		String sName = "";
		byte[] bParamName = new byte[20];
		String[] sParamName = new String[iParamSize];
		for (int i = 0; i < iParamSize; i++) {
			dis.read(bParamName);
			sParamName[i] = new String(bParamName);
			sName += sParamName[i];
		}
		String[] proArrayList = sName.split("          ");
		// System.out.println(sName);

		byte[] bLocationName = new byte[20];
		String[] sLocationName = new String[iLocationSize];
		sName = "";
		for (int i = 0; i < iLocationSize; i++) {
			if (bFileType) {
				dis.read(bVarBUf);
				// System.out.println(ByteBuffer.wrap(b1).order(ByteOrder.LITTLE_ENDIAN).getInt());

				dis.read(bLocationName);
				sLocationName[i] = new String(bLocationName);
				sName += sLocationName[i];
			} else {
				sLocationName[i] = "segment " + df.format(i + 1);
			}
		}

		String rsultSout = "";
		List<RunHisData> efdc1 = new ArrayList<>();
		List<RunHisData> efdc2 = new ArrayList<>();
		List<RunHisData> efdc3 = new ArrayList<>();
		List<RunHisData> efdc4 = new ArrayList<>();
		List<RunHisData> efdc5 = new ArrayList<>();
		int num = 500;

		while (dis.available() > 0) {
			dis.read(bVarBUf);
			int secound = ByteBuffer.wrap(bVarBUf).order(ByteOrder.LITTLE_ENDIAN).getInt();
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(beginTime);
			calendar.add(Calendar.SECOND, secound);
//			System.out.println("--------------1" + DateUtil.dateToString2(calendar.getTime()));
			// 修改2020年
			Calendar date = Calendar.getInstance();
			date.setTime(new Date());
			calendar.set(Calendar.YEAR, date.get(Calendar.YEAR));
//            System.out.println("--------------2"+DateUtil.dateToString2(calendar.getTime()));
			// System.out.println(calendar.getTime());
			// System.out.println("---------------");
			for (int i = 0; i < iLocationSize; i++) {
				rsultSout = sLocationName[i] + "\t";
				String site = sLocationName[i].substring(0, 3);
				String tier = sLocationName[i].substring(6, 7);
				for (int j = 0; j < iParamSize; j++) {
					dis.read(bVarBUf);
					String values = df1.format(ByteBuffer.wrap(bVarBUf).order(ByteOrder.LITTLE_ENDIAN).getFloat());
					RunHisData runHisData = new RunHisData();
					runHisData.setId(HttpTool.getRandomChar(32));
					runHisData.setHisSite(site);
					if (!tier.contains(" ") && tier.length() != 0) {
						runHisData.setHisTier(Integer.parseInt(tier));
					}
					runHisData.setHisPro(proArrayList[j]);
					runHisData.setHisValue(values);
					runHisData.setHisTime(calendar.getTime());
					// runHisData.setHisTime(beginTime);
					runHisData.setFileid(id);
					for (RunProperty runProperty : runPropertiesList) {
						String runSiteRo = proArrayList[j].trim();
						if (runSiteRo.equals(runProperty.getProRemark())) {
							runHisData.setHisRemark(runProperty.getProNamech());
							runHisData.setHisUnit(runProperty.getProUnit());
						}
					}
					rsultSout += values + "\t";
					
					
					if ("SecchiDept".equals(proArrayList[j].trim())) {
						runHisData.setHisPro("Transparence");
						for (RunProperty runProperty : runPropertiesList) {
							if ("Transparence".equals(runProperty.getProNameen())) {
								runHisData.setHisRemark(runProperty.getProNamech());
								runHisData.setHisUnit(runProperty.getProUnit());
								saveListEfdc1(efdc1, efdc2, efdc3, efdc4, efdc5, num, runHisData);
								break;
							}
						}
					} else {
						if ("TOC".equals(runHisData.getHisPro().trim())) {
							saveListEfdc1(efdc1, efdc2, efdc3, efdc4, efdc5, num, runHisData);
								RunHisData ru = new RunHisData();
								ru.setFileid(runHisData.getFileid());
								ru.setHisTime(runHisData.getHisTime());
								ru.setHisSite(runHisData.getHisSite());
								ru.setHisTier(runHisData.getHisTier());
								ru.setId(UUIDUtils.getUUID());
								ru.setHisPro("BOD");
								String hisValue = runHisData.getHisValue();
								double parseDouble = Double.parseDouble(hisValue);
								String format = df1.format(parseDouble * 0.63);
								ru.setHisValue(format);
							for (RunProperty runProperty : runPropertiesList) {
								if ("BOD".equals(runProperty.getProNameen())) {
									ru.setHisRemark(runProperty.getProNamech());
									ru.setHisUnit(runProperty.getProUnit());
									if (StringUtils.isNotBlank(runHisData.getHisRemark())) {
										saveListEfdc1(efdc1, efdc2, efdc3, efdc4, efdc5, num, ru);
									}
									break;
								}
							}
						} else {
							 if("EnCoc".equals(runHisData.getHisPro().trim())
									 ||"EColi".equals(runHisData.getHisPro().trim())
									 ||"FColi".equals(proArrayList[j].trim())){
								 double parseDouble = Double.parseDouble(runHisData.getHisValue());
								 if(parseDouble!=0) {
									 runHisData.setHisValue(df1.format(Math.log10(parseDouble)));
								 }
							}
							if (StringUtils.isNotBlank(runHisData.getHisRemark())) {
								saveListEfdc1(efdc1, efdc2, efdc3, efdc4, efdc5, num, runHisData);
							}
						}
					}
				}

			}

		}
		insertRunHisDataEfdc("run_his_data_year1", efdc1);
		insertRunHisDataEfdc("run_his_data_year2", efdc2);
		insertRunHisDataEfdc("run_his_data_year3", efdc3);
		insertRunHisDataEfdc("run_his_data_year4", efdc4);
		insertRunHisDataEfdc("run_his_data_year5", efdc5);
		dis.close();

	}

	@Transactional(readOnly = false, propagation = Propagation.NEVER)
	private void saveListEfdc1(List<RunHisData> efdc1, List<RunHisData> efdc2, List<RunHisData> efdc3,
			List<RunHisData> efdc4, List<RunHisData> efdc5, int num, RunHisData runHisData) {
		if (runHisData.getHisSite().equals("DM1")) {
			efdc1.add(runHisData);
		} else if (runHisData.getHisSite().equals("DM2")) {
			efdc2.add(runHisData);
		} else if (runHisData.getHisSite().equals("DM3")) {
			efdc3.add(runHisData);
		} else if (runHisData.getHisSite().equals("DM4")) {
			efdc4.add(runHisData);
		} else {
			// 5 和 wate
			efdc5.add(runHisData);
		}

		if (efdc1.size() == num) {
			String tableName = "run_his_data_year1";
			insertRunHisDataEfdc(tableName, efdc1);
			efdc1.clear();
		}
		if (efdc2.size() == num) {
			String tableName = "run_his_data_year2";
			insertRunHisDataEfdc(tableName, efdc2);
			efdc2.clear();
		}
		if (efdc3.size() == num) {
			String tableName = "run_his_data_year3";
			insertRunHisDataEfdc(tableName, efdc3);
			efdc3.clear();
		}
		if (efdc4.size() == num) {
			String tableName = "run_his_data_year4";
			insertRunHisDataEfdc(tableName, efdc4);
			efdc4.clear();
		}
		if (efdc5.size() == num) {
			String tableName = "run_his_data_year5";
			insertRunHisDataEfdc(tableName, efdc5);
			efdc5.clear();
		}
	}

	// 查询一年的数据
	/**
	 * @Author yangyue
	 * @Description //TODO 查询一年的数据
	 * @Date 10:50 2020/12/3
	 * @Param [pro 属性, site （DM1 站点）, tir（层级）, fileID（文件ID）, begintime 开始时间 ,
	 *        endtime 技术时间,type 0 是一年 1 是同化]
	 * @return java.util.List<com.szyjjk.entity.RunHisData>
	 **/
	public List<RunHisData> findOneYData(String pro, String site, String tir, String fileID, Date begintime,
			Date endtime, int type) {
		String tableName = "";
		if (type == 0) {
			switch (site) {
			case "DM1":
				tableName = "run_his_data_year1";
				break;
			case "DM2":
				tableName = "run_his_data_year2";
				break;
			case "DM3":
				tableName = "run_his_data_year3";
				break;
			case "DM4":
				tableName = "run_his_data_year4";
				break;
			case "DM5":
				tableName = "run_his_data_year5";
				break;
			}
		} else if (type == 1) {
			switch (site) {
			case "DM1":
				tableName = "run_his_data_efdc1";
				break;
			case "DM2":
				tableName = "run_his_data_efdc2";
				break;
			case "DM3":
				tableName = "run_his_data_efdc3";
				break;
			case "DM4":
				tableName = "run_his_data_efdc4";
				break;
			case "DM5":
				tableName = "run_his_data_efdc5";
				break;
			}
		}
		return runHisDataMapper.findOneYData(pro, tableName, tir, fileID, begintime, endtime);
	}
	
	/**
	 * 删除his数据  在run_file_path 中没有的his数据  
	 */
	@Transactional(readOnly = false, propagation = Propagation.NEVER)
	public void delNotInRunRilePath() {
		runHisDataMapper.delNotInRunRilePath();
		runHisDataMapper.delMapData1NotInRunRilePath();
		runHisDataMapper.delMapData2NotInRunRilePath();
		runHisDataMapper.delMapData3NotInRunRilePath();
		runHisDataMapper.delMapData4NotInRunRilePath();
		runHisDataMapper.delMapData5NotInRunRilePath();
		runHisDataMapper.delPicNotInRunRilePath();
		runHisDataMapper.delPngNotInRunRilePath();
	}

	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public int updateharewaresite(SysHardWareData data) {
		return sysHardWareDataMapper.updateharewaresite(data);
	}

	public List<SysHardWareData> findSysHarwareDateBysitename(String sitename) {
		return sysHardWareDataMapper.selectbysitename(sitename);
	}

	public List<SysHardWareSimulationData> getmoxingdata(Integer id,String modelName) {

		return sysHardWareDataMapper.getmoxingdata(id,modelName);
	}

	public List<Object> gettongji(Integer id) {
		List<SysHardWareSimulationData> gettongji = sysHardWareDataMapper.gettongji(id);
		List<Object> list = new ArrayList();
		if(gettongji.size()>0){
			for (int i = 0; i < gettongji.size(); i++) {
				List<SysHardWareSimulationData> getmoxingdata = sysHardWareDataMapper.getmoxingdata(id, gettongji.get(i).getModelName());
				if(getmoxingdata.size()>0){
					list.add(getmoxingdata);
				}
			}
		}
		return list;
	}

	// 查询同化的数据

}